package cn.hbu.zlfw.patent.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.FileHandingUtil;
import cn.hbu.zlfw.common.util.Utility;
import cn.hbu.zlfw.dic.entity.DicPatentApplyField;
import cn.hbu.zlfw.dic.service.DicPatentApplyFieldService;
import cn.hbu.zlfw.patent.entity.Patent;
import cn.hbu.zlfw.patent.entity.PatentApply;
import cn.hbu.zlfw.patent.service.PatentApplyService;
import cn.hbu.zlfw.patent.service.PatentService;

/**
 * 
 * @ClassName: PatentApplyController 
 * @Description: 专利交底书控制层
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月3日 上午10:49:29  
 * @version V1.0
 */
@Controller("patentApplyController")
@RequestMapping("/patentApply")
public class PatentApplyController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(PatentApplyController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	/**
	 * 注入service
	 */
	@Autowired
	private PatentApplyService patentApplyService;
	@Autowired
	private PatentService patentService;
	
	@Autowired
	private DicPatentApplyFieldService dicPatentApplyFieldService;
	/**
	 * 
	 * @Title: toPatentApply 
	 * @Description: 填写专利交底书
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="填写专利交底书")
	@RequestMapping("/patentApply.action")
	public String toPatentApply(@RequestParam(value="patentId",required=false) String patentId,ModelMap map) {
		map.put("dicPatentApplyFields", this.dicPatentApplyFieldService.findAll());
		map.put("paramMatchCodeMap", DicPatentApplyField.getParamMatchCodeMap());
		map.put("patent", patentService.getById(patentId));
		PatentApply patentApply = patentApplyService.getNewestApplyByPatentId(patentId);
		if(patentApply != null) {
			String[] uploadFileNames =  patentApply.getUploadFileNames().split(",");
			List<String> fileNames = new ArrayList<String>();
			for (int i = 0; i < uploadFileNames.length; i++) {
				String[] tempfileNames = uploadFileNames[i].split("_");
				if(tempfileNames.length>1) {
					fileNames.add(tempfileNames[1]);
				}
			}
			patentApply.setUploadFileNames(Utility.getListString(fileNames));
			map.put("patentApply", patentApply);
		}
		return "zlxt/patentApply";
	}
	/**
	 * 
	 * @Title: commitApplyForm 
	 * @Description: 提交专利交底书
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SAVE,description="成功保存专利交底书")
	@RequestMapping("/commitApply.action")
	public String commitApplyForm(@ModelAttribute("patentApply") PatentApply patentApply,
			@RequestParam("patentName") String patentName,
			@RequestParam(value="patentId",required=false) String patentId,
			@RequestParam(value="editorValue",required=false) String editorValue,
			@RequestParam(value="uploadfiles",required=false) List<MultipartFile> uploadfiles) {
		//更新专利名字
		Patent patent=this.patentService.getById(patentId);
		patent.setName(patentName);
		patent.setSfApply(1);         		//已提交专利交底书
		patentApply.setPatent(patent);		//维护一对多关系
		this.patentService.update(patent);
		//处理上传文件
		List<MultipartFile> files = uploadfiles;
		if(null != files && files.size() > 0) {
			List<String> fileNames = new ArrayList<String>();
			for (MultipartFile multipartFile : files) {
				String fileName = FileHandingUtil.uploadFile(multipartFile, request);
				if(null != fileName) {
					fileNames.add(fileName);
				}
			}//end foreach
			//处理文件名册存储,两个文件名之间用逗号隔开
			patentApply.setUploadFileNames(Utility.getListString(fileNames));
		}//end if
		if(editorValue!=null) {
			patentApply.setPictexplain(editorValue);
		}
		patentApply.setCreateTime(new Date());
		patentApply.setUpdateTime(new Date());
		patentApply.setDelState(0);
		this.patentApplyService.save(patentApply);
		return "forward:/patent/topatentlist.action";
	}
}
